package com.ethannhu.todaynews.component.splash

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import com.ethannhu.superui.date.SuperDateUtil
import com.ethannhu.todaynews.component.main.MainActivity
import com.ethannhu.todaynews.R
import com.ethannhu.todaynews.activity.BaseViewModelActivity
import com.ethannhu.todaynews.component.ad.AdActivity
import com.ethannhu.todaynews.component.guide.GuideActivity
import com.ethannhu.todaynews.databinding.ActivitySplashBinding
import com.ethannhu.todaynews.util.DefaultPreferenceUtil
import com.ethannhu.todaynews.util.IntentUtil
import com.ethannhu.todaynews.util.PreferenceUtil
import com.permissionx.guolindev.PermissionX

@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseViewModelActivity<ActivitySplashBinding>() {
    override fun initData() {
        super.initData()
        //设置版权年份
        val currentYear = SuperDateUtil.currentYear()
        binding.copyright.text = getString(R.string.copyright, currentYear)

        if (!DefaultPreferenceUtil.getInstance(this).isAcceptTermsServiceAgreement()) {
            showTermsServiceAgreementDialog()
        } else {
            //已经同意了协议
            requestPermission()
        }

    }

    /**
     * 显示同意服务条款对话框
     */
    private fun showTermsServiceAgreementDialog() {
        TermsServiceDialogFragment.show(supportFragmentManager) {
            //给 sp 写已同意
            DefaultPreferenceUtil.getInstance(this).setAcceptTermsServiceAgreement()
            requestPermission()
        }
    }

    /**
     * 申请用户权限
     */
    private fun requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            PermissionX.init(this)
                .permissions(
                    android.Manifest.permission.CAMERA,
                    android.Manifest.permission.RECORD_AUDIO,
                    android.Manifest.permission.ACCESS_COARSE_LOCATION,
                    android.Manifest.permission.ACCESS_FINE_LOCATION,
                    android.Manifest.permission.READ_PHONE_STATE,
                    android.Manifest.permission.READ_MEDIA_AUDIO,
                    android.Manifest.permission.READ_MEDIA_IMAGES,
                    android.Manifest.permission.READ_MEDIA_VIDEO,

                    )
        } else {
            PermissionX.init(this)
                .permissions(
                    android.Manifest.permission.CAMERA,
                    android.Manifest.permission.RECORD_AUDIO,
                    android.Manifest.permission.ACCESS_COARSE_LOCATION,
                    android.Manifest.permission.ACCESS_FINE_LOCATION,
                    android.Manifest.permission.READ_PHONE_STATE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    android.Manifest.permission.READ_EXTERNAL_STORAGE,

                    )
        }
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) { //如果所有权限都允许了
                    binding.root.postDelayed({
                        prepareNext()  //调到引导界面
                    }, 1000)
                } else {
                    //可以在这弹出提示告诉用户为什么需要权限
                    finish()
                }

            }
    }

    private fun prepareNext() {
        if (!DefaultPreferenceUtil.getInstance(this).isSetShowGuide()) {
            startActivityAfterFinishThis(GuideActivity::class.java)
        } else {
            startActivityAfterFinishThis(MainActivity::class.java)
        }

        val intent = Intent()
        if (PreferenceUtil.isLogin()) {
            intent.setClass(hostActivity, AdActivity::class.java)
        } else {
            intent.setClass(hostActivity, MainActivity::class.java)
        }

        IntentUtil.cloneIntent(getIntent(), intent)

        startActivity(intent)

        finish()

        //禁用启动界面动画
        overridePendingTransition(0, 0)
    }
}

